/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daos;

import beans.Bean;
import beans.ConsultorioBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Milene
 */
public class MSSQLConsultorioDao extends MSSQLDao implements Dao{

    public MSSQLConsultorioDao() throws Exception {
        
        super();
    }
    
          


    @Override
    public Bean make(ResultSet result) throws Exception {
       
        ConsultorioBean cb = new ConsultorioBean();
        cb.setNombreConsultorio(result.getString("nombre"));
        cb.setUbicacionConsultorio(result.getString("ubicacion"));
        
        return cb;
    }

    @Override
    public int insert(Bean bean) throws Exception {
       
        try{
            this.getConnection().setAutoCommit(false);
            this.setStatement("insertar_consultorio(?,?)");
            this.getStatement().setString(1, ((ConsultorioBean) bean).getNombreConsultorio());
            this.getStatement().setString(2, ((ConsultorioBean) bean).getUbicacionConsultorio());
            this.getStatement().executeUpdate();

        } catch (SQLException ex) {
            this.getConnection().rollback();
            throw ex;
        } catch (Exception ex2) {
            this.getConnection().rollback();
            throw ex2;
        } finally {
            this.getConnection().setAutoCommit(true);
        }
        return 0;
    }


    
    @Override
    public int delete(Bean bean) throws Exception {
        int rs = 0;
        try {
            this.getConnection().setAutoCommit(false);//para mantener la integridad de la base, usar transaccion

            this.setStatement("eliminar_consultorio(?)");
            this.getStatement().setString(1, ((ConsultorioBean) bean).getNombreConsultorio());

            this.getStatement().executeUpdate();
            
            this.getConnection().commit();

            
        } catch (SQLException ex) {
            this.getConnection().rollback();
            throw ex;
        } catch (Exception ex2) {
            this.getConnection().rollback();
            throw ex2;
        } finally {
            this.getConnection().setAutoCommit(true);
        }
        return rs;

    }

    @Override
    public List<Bean> select(Bean bean) throws Exception {
        
        ResultSet result;
        List<Bean> consultorios=new ArrayList<Bean>();
        this.setStatement("get_consultorio");
        result=this.getStatement().executeQuery();
        while(result.next()){
            
            consultorios.add(this.make(result));
        }
        
        return consultorios;
    }


   

    @Override
    public boolean valid(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

  

    @Override
    public Bean get(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public long getSize(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int update(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

  
}
